package com.fh.qqclient.view;

import com.fh.qqclient.service.MessageClientService;
import com.fh.qqclient.service.UserClientService;
import com.fh.qqclient.utils.Utility;

import java.io.IOException;

/**
 * @author 方小勤
 * @version 1.0~
 * 客户端的菜单界面
 */
public class QQview{
    private boolean loop = true;  //控制是否显示菜单
    private String key = ""; //接受用户的键盘输入
    private UserClientService userClientService = new UserClientService();//对象是用于登录服务/注册用户
    private MessageClientService messageClientService = new MessageClientService();//用于消息的发送
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        new QQview().mianMenu();
        System.out.println("客户端退出系统 ");
    }
    public void mianMenu() throws IOException, ClassNotFoundException {
        while(loop){
            System.out.println("=========欢迎登录网络通信系统=========");
            System.out.println("\t\t 1 登录系统");
            System.out.println("\t\t 9 退出系统");
            System.out.println("请输入你的选择:");
            key = Utility.readString(1);
            switch (key){
                case "1":
                    System.out.println("登录系统");
                    System.out.println("请输入用户号： ");
                    String userId = Utility.readString(50);
                    System.out.println("请输入密 码: ");
                    String pwd = Utility.readString(50);
                    //这里就需要访问客户端验证是否合法了
                    if(userClientService.checkUser(userId,pwd)){
                        System.out.println("=========欢迎（用户 "+ userId + " 登录成功 )==========");
                        //进入二级菜单
                        while(loop){
                            System.out.println("\n=========网络通信系统的二级菜单（用户 "+ userId + " )==========");
                            System.out.println("\t\t 1 显示在线用户列表");
                            System.out.println("\t\t 2 群发消息");
                            System.out.println("\t\t 3 私聊消息");
                            System.out.println("\t\t 9 退出系统");
                            System.out.println("请输入你的选择： ");
                            key = Utility.readString(1);
                            switch (key){
                                case "1":
                                   userClientService.onlineFriedList();
                                    break;
                                    case "2":
                                        System.out.println("请输入相对大家说的话： ");
                                        String s = Utility.readString(100);
                                        messageClientService.sendMessageToALL(s,userId);
                                        break;
                                        case "3":
                                            System.out.println("请输入想聊天的用户号（在线）： ");
                                            String getterId = Utility.readString(50);
                                            System.out.println("请输入想说的话：");
                                            String content = Utility.readString(100);
                                            messageClientService.sendMessageToOne(content,userId,getterId);
                                            break;
                                                case "9":
                                                    userClientService.logout();
                                                    loop = false;
                                                    break;
                            }
                        }
                    }else{
                        System.out.println("========登录失败========");
                    }
                    break;
                case "9":
                    loop = false;
                    break;
            }
        }
    }
}
